import {ElLoading, ElMessage} from 'element-plus';

const apiContextPath = import.meta.env.VITE_API_CONTEXT_PATH;

/**
 * axios配置 与 AxiosBuilder默认配置
 */
export default {
  // axios实例配置
  baseUrl: apiContextPath ? apiContextPath : '/',
  timeout: 60 * 1000 * 10, // 单位ms
  
  // 加载相关
  loading: true,
  showLoading() {
    ElLoading.service({
      lock: true,
      text: '加载中...',
    });
  },
  hideLoading() {
    ElLoading.service({}).close();
  },
  
  // 请求相关
  requestBodyFormUrlEncoded: false,
  addRequestHeaders(headers) {
    // 此处常用于添加权限验证token请求头，添加逻辑根据不同项目定制化！
    // tokenInfo {'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxNjUxNDYzMjc0ODkxNDQ4MzIwIiwicm5TdHIiOiJIVFVKWktwSldHUEY3OFY1ejk0a2NiMUVpUURQcDhoayJ9.yRTHby8kDlYy78fYdfrU3XhJmKO4P4-cn8B2YTDZBIo'}
    // const tokenInfo = useGeneralStore()?.tokenInfo;
    // if (tokenInfo.value) {
    //   headers[tokenInfo.key] = `Bearer ${tokenInfo.value}`;
    // }
  },
  
  // 响应相关
  completeResponse: false, // 为true则响应axiosResponse，为false则响应axiosResponse.data
  responseBusinessCode: {
    success: '00000',
    tokenTimeout: 'A0301',
    tokenInvalid: 'A0302',
    tokenNotFound: 'A0303',
  },
  
  // token验证失败后的处理
  handleTokenInvalid() {
    // 此处常用于退出登录并跳转登录页
  },
  
  // 业务成功消息提示
  showSuccessMsg: true,
  showSuccessMsgFunction(msg) {
    ElMessage.success(msg);
  },
  
  // 业务失败消息提示
  showFailureMsg: true,
  showFailureMsgFunction(msg) {
    ElMessage.error(msg);
  },
};